package com.xiaohong.order.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaohong.order.interceptor.TokenRequestInterceptor;
import com.xiaohong.order.pojo.OrderItem;
import com.xiaohong.order.service.OrderItemService;
import entity.Result;
import entity.StatusCode;
import entity.TokenDecode;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Tag(name = "OrderItem接口文档")
@RestController
@RequestMapping("/orderItem")
@CrossOrigin
public class OrderItemController {

    @Autowired
    private OrderItemService orderItemService;

    /***
     * 分页条件搜索实现OrderItem
     * @param orderItem
     * @param page
     * @param size
     * @return
     */
    @Operation(summary = "条件分页查询OrderItem",description = "所需权限：")
    @Parameters({
            @Parameter(in = ParameterIn.PATH, name = "page", description = "当前页", required = true),
            @Parameter(in = ParameterIn.PATH, name = "size", description = "每页显示条数", required = true)
    })
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<Page> findPage(@RequestBody(required = false) @Parameter(name = "OrderItem对象",required = true) OrderItem orderItem, @PathVariable int page, @PathVariable  int size){
        //调用OrderItemService实现分页条件查询OrderItem
        Page pageInfo = orderItemService.findPage(orderItem, page, size);
        return new Result<Page>(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * 分页搜索实现OrderItem
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @Operation(summary = "分页查询OrderItem",description = "所需权限：")
    @Parameters({
            @Parameter(in = ParameterIn.PATH, name = "page", description = "当前页", required = true),
            @Parameter(in = ParameterIn.PATH, name = "size", description = "每页显示条数", required = true)
    })
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<Page> findPage(@PathVariable  int page, @PathVariable  int size){
        //调用OrderItemService实现分页查询OrderItem
        Page pageInfo = orderItemService.findPage(page, size);
        return new Result<Page>(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * 多条件搜索OrderItem数据
     * @param orderItem
     * @return
     */
    @Operation(summary = "多条件查询OrderItem",description = "所需权限：")
    @PostMapping(value = "/search" )
    public Result<List<OrderItem>> findList(@RequestBody(required = false) @Parameter(name = "OrderItem对象",required = true) OrderItem orderItem){
        //调用OrderItemService实现条件查询OrderItem
        List<OrderItem> list = orderItemService.findList(orderItem);
        return new Result<List<OrderItem>>(true,StatusCode.OK,"查询成功",list);
    }

    /***
     * 根据Id删除orderItem数据
     * @param id
     * @return
     */
    @Operation(summary = "根据ID删除OrderItem",description = "所需权限：")
    @Parameter(in = ParameterIn.PATH, name = "id", description = "ID", required = true)
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable String id){
        //调用OrderItemService实现根据ID删除
        orderItemService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /***
     * 根据ID修改OrderItem数据
     * @param orderItem
     * @param id
     * @return
     */
    @Operation(summary = "根据ID修改OrderItem",description = "所需权限：")
    @Parameter(in = ParameterIn.PATH, name = "id", description = "ID", required = true)
    @PutMapping(value = "/{id}" )
    public Result update(@RequestBody @Parameter(name = "OrderItem对象",required = true) OrderItem orderItem, @PathVariable String id){
        // 补充id
        orderItem.setId(id);
        //调用OrderItemService实现修改OrderItem
        orderItemService.update(orderItem);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /***
     * 新增OrderItem数据
     * @param orderItem
     * @return
     */
    @Operation(summary = "添加OrderItem",description = "所需权限：")
    @PostMapping
    public Result add(@RequestBody  @Parameter(name = "OrderItem对象",required = true) OrderItem orderItem){
        //调用OrderItemService实现添加OrderItem
        orderItemService.add(orderItem);
        return new Result(true,StatusCode.OK,"添加成功");
    }

    /***
     * 根据ID查询OrderItem数据
     * @param id
     * @return
     */
    @Operation(summary = "根据ID查询OrderItem",description = "所需权限：")
    @Parameter(in = ParameterIn.PATH, name = "id", description = "ID", required = true)
    @GetMapping("/{id}")
    public Result<OrderItem> findById(@PathVariable String id){
        //调用OrderItemService实现根据ID查询OrderItem
        OrderItem orderItem = orderItemService.findById(id);
        return new Result<OrderItem>(true,StatusCode.OK,"查询成功",orderItem);
    }

    /***
     * 查询自己的OrderItem数据
     * @param httpServletRequest
     * @return
     */
    @Operation(summary = "查询自己的OrderItem数据",description = "所需权限：")
    @Parameter(in = ParameterIn.PATH, name = "id", description = "ID", required = true)
    @GetMapping("/self")
    public Result<List<OrderItem>> findBySelf(HttpServletRequest httpServletRequest) {
        //获取token
        String token = httpServletRequest.getHeader("Authorization");
        TokenRequestInterceptor.token = token;

        //获取用户令牌信息
        String username = (String) TokenDecode.getUserInfo().get("username");

        //调用OrderItemService实现查询OrderItem
        OrderItem orderItem1 = new OrderItem();
        orderItem1.setUserId(username);
        List<OrderItem> list = orderItemService.findList(orderItem1);
        return new Result<List<OrderItem>>(true, StatusCode.OK,"查询成功",list) ;
    }

        /***
         * 查询OrderItem全部数据
         * @return
         */
    @Operation(summary = "查询所有OrderItem",description = "所需权限：")
    @GetMapping
    public Result<List<OrderItem>> findAll(){
        //调用OrderItemService实现查询所有OrderItem
        List<OrderItem> list = orderItemService.findAll();
        return new Result<List<OrderItem>>(true, StatusCode.OK,"查询成功",list) ;
    }
}
